package com.example.graduation.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduation.entity.Address;
import com.example.graduation.exception.MyException;
import com.example.graduation.mapper.AddressMapper;
import com.example.graduation.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.graduation.util.JwtUtil;
import com.example.graduation.util.R;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {


    @Resource
    private AddressMapper addressMapper;

    @Resource
    private JwtUtil jwtUtil;


    /**
     * 新增或者修改自己的收获地址
     *
     * @param address
     * @param token
     * @return {@link Address}
     * @author xieyucan
     * CreateDate 2022/3/23 10:14
     */
    @Override
    public Address updateAddress(Address address, String token) {
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            if (ObjectUtil.isEmpty(address.getId())) {
                address.setUserId(userId);
                int insert = addressMapper.insert(address);
                if (insert > 0) {
                    return address;
                } else {
                    throw new MyException("添加失败");
                }
            } else {
                if (address.getUserId() == userId) {
                    UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
                    wrapper.eq("id", address.getId());
                    int update = addressMapper.update(address, wrapper);
                    if (update > 0) {
                        return address;
                    } else {
                        throw new MyException("修改失败");
                    }
                } else {
                    throw new UnauthorizedException("权限不足");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 查询自己的收获地址
     *
     * @param token
     * @return {@link Page< Address>}
     * @author xieyucan
     * CreateDate 2022/3/23 10:14
     */
    @Override
    public Page<Address> findAddress(String token, Integer page, Integer size) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 5;
        }
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            QueryWrapper<Address> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            Page<Address> addressPage = addressMapper.selectPage(new Page<>(page, size), wrapper);
            return addressPage;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败");
        }
    }


    /**
     * 删除地址
     *
     * @param token
     * @param addressId
     * @return
     */
    public boolean deleteAddress(String token, Integer addressId) {
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            Address address = addressMapper.selectById(addressId);
            if (address.getUserId() == userId) {
                addressMapper.deleteById(addressId);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new MyException("删除失败");
        }

    }
}
